package com.example.demo.services

import com.example.demo.model.Users
import com.example.demo.model.dto.UsersInput
import org.babyfish.jimmer.sql.kt.KSqlClient
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/u")
class UserService(
    private val sqlClient: KSqlClient
) {

    @GetMapping("/")
    fun getAll() =
        sqlClient.entities.findAll(Users::class)

    @GetMapping("/{id}")
    fun getOne(@PathVariable id:Long) =
        sqlClient.entities.findById(Users::class, id)

    @PostMapping("/")
    fun add(@RequestBody user: Users) =
        sqlClient.entities.save(user).modifiedEntity


    @PutMapping("/")
    fun update(@RequestBody user: Users) =
        sqlClient.entities.save(user).modifiedEntity

    @DeleteMapping("/{id}")
    fun removeOne(@PathVariable id: Long) =
        sqlClient.entities.delete(Users::class, id).totalAffectedRowCount

    @GetMapping("/xxx")
    fun xxx(@RequestBody userInput: UsersInput) =
        sqlClient.save(userInput).modifiedEntity

}